package movies.repositoryHibernate;

import java.util.List;
import movies.domain.MovieForPerson;
import movies.repository.IMovieForPersonRepository;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hanna_Aliakseichykava
 */
public class MovieForPersonRepository extends HibernateDaoSupport
        implements IMovieForPersonRepository{

    @Override
    public MovieForPerson getMovieForPersonById(int id) {
        List < MovieForPerson > movieForPersons = getHibernateTemplate().
             findByNamedQuery("GetMovieForPersonById", id);

        if (movieForPersons.size() == 1) {
            return movieForPersons.get(0);
        } else {
            return null;
        }
    }

    @Override
    public MovieForPerson saveMovieForPerson(MovieForPerson movieForPerson) {
        Integer id = (Integer)getHibernateTemplate().save(movieForPerson);
         movieForPerson.setId(id);
         return movieForPerson;
    }

    @Override
    public void deleteMovieForPerson(MovieForPerson movieForPerson) {
        getHibernateTemplate().delete(movieForPerson);
    }

}
